package leetcode.数组;

import javax.swing.tree.TreeNode;

public class 数组转二叉搜索树 {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static TreeNode sortedArrayToBST(int[] nums) {
        return nums==null?null:buildTree(nums,0,nums.length-1);
    }

    public static TreeNode buildTree(int[] nums,int L,int R){
        if(L > R)
            return null;
        int mid = L + ((R-L)>>1);
        TreeNode root = new TreeNode(nums[mid]);
        root.left = buildTree(nums,L,mid-1);
        root.right = buildTree(nums,mid+1,R);
        return root;
    }




}
